/**
 * @descrition cnode模块api
 * @author anytao
 * @created 2016-09-01
 * @version 1.0
 */
'use strict';
import Base from './base.js';
export default class extends Base {

    /**
     * 列表页数据
     */
    async topicslistAction() {

        let result = {};

        let page = this.get('page');
        let per_page = this.get('per_page');

        let utilService = think.service('util');
        let utilServiceInstance = new utilService();

        let cnodeApiService = think.service('cnodeapi');
        let cnodeApiServiceInstance = new cnodeApiService();

        let topicsData = await cnodeApiServiceInstance.getTopics(page, '', per_page);

        let topics = JSON.parse(topicsData).data;

        for (const topic of topics) {

            //获取一张图片作为封面
            let imgArray = topic.content.match(/dn-cnode.qbox.me\/[/\S]{28}/);
            topic.img = '/static/img/cnode.png';
            topic.height = 75;
            if (imgArray != null) {
                topic.img = '//' + imgArray[0];
                let imgSize = await utilServiceInstance.getImageSize('http:' + topic.img);
                topic.height = 300 * imgSize.height / imgSize.width;
            }

            if (topic.content.length <= 500) {
                topic.description = topic.content;
            } else if (topic.content.length <= 10000) {
                topic.description = topic.content.substr(0, Math.round(topic.content.length * 0.2));
            } else {
                topic.description = topic.content.substr(0, Math.round(topic.content.length * 0.1));
            }
        }

        result.items = topics;
        result.pagenum = 1000; //总页数
        result.nextpage = page;
        return this.json(result);
    }
}